"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : tpcc执行过程中gs_ctl stop 使用fast方式在主机停止集群
Create At   : 2020/07/24
@zou_jialiang0505328126
Description :
    1. 执行TPCC
    2.执行TPCC过程中stop主机
    3.重启数据库
    4.循环步骤1.2.3 10次
    5.查询数据库状态
    6.执行TPCC，检查数据库是否可正常使用
Expect      :
    1. 执行TPCC
    2.执行TPCC过程中stop主机
    3.重启数据库
    4.循环步骤1.2.3 10次
    5.查询数据库状态
    6.执行TPCC，检查数据库是否可正常使用
History     :
    modified:2021-2-5 by 5328113;修改用例逻辑，增加主备同步等待；增加必要打印信息
    modified:2021/02/26 by @wan005:数据量大的情况下，重启数据库主备同步时间增加，增加同步时间
"""
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Stop_immediate(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----this is setup----')
        self.com = Common()
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()
        self.log.info('----Opengauss_Reliability_Dbsys_Case124 初始化----')

    def test_stop_immediate(self):
        for i in range(1, 10):
            self.log.info('----第' + str(i) + '次循环----')

            self.log.info('----第' + str(i) + '次循环执行TPCC----')
            tpcc_thread = ComThread(self.com.startTPCC,
                                    args=(self.pri_root, macro.TPCC_PATH,))
            tpcc_thread.setDaemon(True)
            tpcc_thread.start()

            time.sleep(10)
            self.log.info('----第' + str(i) + '次循环stop主机----')
            success_msg = self.constant.GS_CTL_STOP_SUCCESS_MSG
            stop_result = self.pri_sh.executeGsctl('stop', success_msg,
                                                   '-m fast -t 900')
            self.assertTrue(stop_result)

            self.log.info('----第' + str(i) + '次查询主备状态----')
            self.pri_sh.getDbClusterStatus('status')

            self.log.info('----第' + str(i) + '次查询tpcc执行结果----')
            tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
            tpcc_result = tpcc_thread.get_result()
            self.log.info(tpcc_result)
            self.assertTrue(tpcc_result.find('closed') > -1)

            self.log.info('----第' + str(i) + '次循环start主机----')
            success_msg = self.constant.RESTART_SUCCESS_MSG
            start_result = self.pri_sh.executeGsctl('start', success_msg,
                                                   '-t 900 -M primary')
            self.assertTrue(start_result)

            self.log.info('----第' + str(i) + '次查看主机query，同步是否正常----')
            result = self.pri_sh.check_location_consistency('primary', 3)
            if not result:
                result = self.pri_sh.check_location_consistency('primary', 3)
                self.assertTrue(result)

            self.log.info('----第' + str(i) + '次查询主备状态----')
            status = self.pri_sh.getDbClusterStatus('status')
            self.assertTrue(status)

        self.log.info('------循环结束后start TPCC----')
        tpcc_result = self.com.startTPCC(self.pri_root, macro.TPCC_PATH)
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('----查看主机query，同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)

        self.log.info('----查询主备状态----')
        status = self.pri_sh.getDbClusterStatus('status')
        self.assertTrue(status)

    def tearDown(self):
        self.log.info('--------this is tearDown---------')
        self.log.info('----Opengauss_Reliability_Dbsys_Case124 执行结束----')
